// 这个文件是由多个文件合并出来的，不要直接修改该文件
// 子文件清单


// utils.js 系统用到的一些辅助方法

////////////////////////////////////////////////////////////////////////////////
// 以post方式打开新窗口,主要用于打印和导出excel传递参数的时候
if(Ext.isChrome){
//chrome
  window.openPostWindow = function(url,name,data){
      window.open(url+'?'+Ext.urlEncode(data),name,'height=400, width=400, top=0, left=0, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, status=yes');   
  };
} else {
// FF && IE
window.openPostWindow=function(url,name,data){ 
    var tempForm = document.createElement("form"); 
    tempForm.id="tempForm1"; 
    tempForm.method="post"; 
    tempForm.action=url; 
    tempForm.target=name; 
    for(var p in data){ 
        var hideInput = document.createElement("input"); 
        hideInput.type="hidden"; 
        hideInput.name= p;
        hideInput.value= data[p];
        tempForm.appendChild(hideInput);  
    }
    var request_forgery_protection_token_hideInput = document.createElement("input"); 
    request_forgery_protection_token_hideInput.type="hidden"; 
    request_forgery_protection_token_hideInput.name= "authenticity_token";
    request_forgery_protection_token_hideInput.value= Ext.Ajax.extraParams.authenticity_token;
    tempForm.appendChild(request_forgery_protection_token_hideInput);  

    if(tempForm.attachEvent){
        tempForm.attachEvent("onsubmit",function(){openWindow(name);});
    } else {
        tempForm.addEventListener("submit",function(){openWindow(name);},false); 
    }
    document.body.appendChild(tempForm); 
    if (tempForm.fireEvent) {
        tempForm.fireEvent('onsubmit');
        tempForm.submit();
    } else if (document.createEvent) {
        var ev = document.createEvent('HTMLEvents');
        ev.initEvent('submit', false, true);
        tempForm.dispatchEvent(ev);
    }
    
    document.body.removeChild(tempForm);
    return false;
};

}

////////////////////////////////////////////////////////////////////////////////
// 四舍五入,由参数决定精确度
Math.roundNumber = function (num, dec) {
  var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
  return result;
};
//  四舍五入,精确到销售点后两位
Math.round2 = function(num) {
  var result = Math.round(num*100)/100;
  return result;
};


// combo renderer,主要用于在表格中输出
Ext.util.Format.comboRenderer = function(combo){
  return function(value){
    var record = combo.findRecord(combo.valueField, value);
    return record ? record.get(combo.displayField) : combo.valueNotFoundText;
  };
};



////////////////////////////////////////////////////////////////////////////////
Ext.ns('Ext.util.form');

//让一个panel的工具条处于禁用状态
Ext.util.form.disablePanel = function(panel){
  Ext.each(panel.getTopToolbar().items.items, function(item,index,length){
      if(item.setDisabled){
          item.setDisabled(true);
      }
  });
  Ext.each(panel.getBottomToolbar().items.items, function(item,index,length){
      if(item.setDisabled){
          item.setDisabled(true);
      }
  });
};

//让一个form处于不可编辑状态
Ext.util.form.disableForm = function(form){
  form.getForm().items.each(function(item,index,length){
      if(item.setReadOnly){
          item.setReadOnly(true);
      }
  });
  Ext.util.form.disablePanel(form);
};

////////////////////////////////////////////////////////////////////////////////
// 弹出一个提示框，一秒钟后自动消失
//copy and edit from Ext.example
Ext.ns('App');
App.message = function(){
    var msgCt;
    function createBox(t, s){
        return ['<div class="msg">',
                '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
                '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>', t, '</h3>', s, '</div></div></div>',
                '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
                '</div>'].join('');
    }
    return {
        popup : function(title, format){
            if(!msgCt){
                msgCt = Ext.DomHelper.insertFirst(document.body, {id:'msg-div'}, true);
            }
            msgCt.alignTo(document, 't-t');
            var s = String.format.apply(String, Array.prototype.slice.call(arguments, 1));
            var m = Ext.DomHelper.append(msgCt, {html:createBox(title, s)}, true);
            m.slideIn('t').pause(1).ghost("t", {remove:true});
        },
        popup_warn : function(title, format){
            if(!msgCt){
                msgCt = Ext.DomHelper.insertFirst(document.body, {id:'msg-div'}, true);
            }
            msgCt.alignTo(document, 'c-c');
            var s = String.format.apply(String, Array.prototype.slice.call(arguments, 1));
            var m = Ext.DomHelper.append(msgCt, {html:createBox(title, s)}, true);
            m.slideIn('t').frame("ff0000", 3, { duration: 1 }).ghost("t", {remove:true});
        },
        init : function(){
        }
    };
}();

var export_to_excel = function(options){
  var grid = options.grid;
  var formpanel = options.formpanel;
  var url = options.url;
  return function(){
      params = (formpanel && formpanel.getForm().getValues()) || {};
      if(formpanel) {
	      var datefields = formpanel.findByType('compositefield');
	      Ext.each(datefields,function(field){
	    	  field.items.each(function(item,index,length){
	    		  if(Ext.isEmpty(item.getValue())){
	    			  params[item.name] = '';
	    		  }
	    	  });
	      });
  	  }
      if(grid.store.sortInfo && grid.store.remoteSort){
          var pn = grid.store.paramNames;
          params[pn.sort] = grid.store.sortInfo.field;
          params[pn.dir] = grid.store.sortInfo.direction;
      }
      var cm = grid.getColumnModel();
      var columns = [];
      var objTemp;
      for (var i = 0; i < cm.getColumnCount(); i++) {
          if (cm.getDataIndex(i)){
        	  /*var fld = grid.store.recordType.prototype.fields.get(cm.getDataIndex(i));
              var column_type = 'String';
              switch(fld.type) {
                  case "int":
                      column_type="int";
                      break;
                  case "float":
                      column_type="float";
                      break;
                  case "date":
                      column_type="date";
                      break;
              };*/
              //params['columns['+i+'][hidden]']= cm.isHidden(i);
              //params['columns['+i+'][column_width]'] = cm.getColumnWidth(i);
              //params['columns['+i+'][column_header]']= cm.getColumnHeader(i);
              //params['columns['+i+'][column_type]']  = column_type;
              //params['columns['+i+'][data_index]']  = cm.getDataIndex(i);
              objTemp = {/*columnWidth:cm.getColumnWidth(i),*/ columnHeader: cm.getColumnHeader(i),
            		  dataIndex: cm.getDataIndex(i)};
              if(!Ext.isEmpty(objTemp)){
            	  columns.push(Ext.encode(objTemp));
              }
          }
      }
      params.columns=Ext.encode(columns);
      //console.log(Ext.encode(params));
      openPostWindow(url,'_blank',params);
  }
};

